package com.hxm.drawview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import com.hxm.drawview.Utils.ScreenUtil;
import com.hxm.drawview.Utils.SizeUtil;

/**
 * Created by hxm on 2017/11/14.
 * 描述：
 */
public class NovelPageView extends View {

    private TextPaint textPaint;
    private Paint batteryPaint, batteryPaint1;
    private Paint paint;
    private String text;
    private Rect rTitle, rContent, rTime, rBattery, rBatteryInner;
    private int screenWidth;
    private int screenHeight;
    private int fontSize;
    private int titleSize;
    //左右
    private int lr_margin;
    //上下
    private int tb_margin;
    private int baseLineX;
    //顶部的文字BaseLine
    private int baseLineY_TOP;
    //底部的文字BaseLine
    private int baseLineY_BOTTOM;
    //字符串高度
    private int textHeight;

    public NovelPageView(Context context) {
        this(context, null);
    }

    public NovelPageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NovelPageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //文本内容
        textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(ContextCompat.getColor(context, R.color.textColor));
        textPaint.setTextSize(SizeUtil.dp2px(context, 18));
        textPaint.setTextAlign(Paint.Align.LEFT);
        //标题、时间
        paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(ContextCompat.getColor(context, R.color.textColor));
        paint.setTextSize(SizeUtil.dp2px(context, 12));
        paint.setTextAlign(Paint.Align.LEFT);
        //电池
        batteryPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        batteryPaint.setColor(ContextCompat.getColor(context, R.color.textColor));
        batteryPaint.setStrokeWidth(2);
        batteryPaint.setStyle(Paint.Style.STROKE);

        batteryPaint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
        batteryPaint1.setColor(ContextCompat.getColor(context, R.color.textColor));
        batteryPaint1.setStyle(Paint.Style.FILL);

        text = context.getString(R.string.novel_text);

        screenWidth = ScreenUtil.getScreenWidth(context);
        screenHeight = ScreenUtil.getScreenHeight(context);
        fontSize = SizeUtil.sp2px(context, 16);
        titleSize = SizeUtil.sp2px(context, 12);
        lr_margin = SizeUtil.dp2px(context, 10);
        tb_margin = SizeUtil.dp2px(context, 5);

        baseLineX = lr_margin;
        Paint.FontMetricsInt metricsInt = paint.getFontMetricsInt();
        baseLineY_TOP = tb_margin - metricsInt.top;
        baseLineY_BOTTOM = screenHeight - tb_margin - metricsInt.bottom;
        textHeight = metricsInt.bottom - metricsInt.top;
        //电池绘制参数
        int batteryWidth = SizeUtil.dp2px(context, 20);
        int batteryHeight = SizeUtil.dp2px(context, 10);
        int space = SizeUtil.dp2px(context, 1);
        int bL = screenWidth - lr_margin - batteryWidth;
        int bT = screenHeight - tb_margin - batteryHeight;
        int bR = screenWidth - lr_margin;
        int bB = screenHeight - tb_margin;
        rBattery = new Rect(bL, bT, bR, bB);
        rBatteryInner = new Rect(bL + space, bT + space, bR - space, bB - space);
        //TODO 计算显示多少行

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawTitle(canvas);
        drawContent(canvas);
        drawTimeAndProcess(canvas);
        drawBattery(canvas);
    }

    private void drawBattery(Canvas canvas) {
        canvas.drawRect(rBattery, batteryPaint);
        canvas.drawRect(rBatteryInner, batteryPaint1);
    }

    private void drawTimeAndProcess(Canvas canvas) {
        canvas.drawText("1/12", baseLineX, baseLineY_BOTTOM, paint);
    }

    private void drawTitle(Canvas canvas) {
        canvas.drawText("第一章 突破至斗帝", baseLineX, baseLineY_TOP, paint);
    }

    private void drawContent(Canvas canvas) {

    }
}
